<?php

App::uses('AdminController', 'Controller');

class AdminPartnersController extends AdminController{
    
    public $uses = array('Partner');
    
    public function index(){
        $partners = $this->Partner->find('all');
        $this->set('allPartners', $partners);
        
    }
    
    public function create() {
        $this->set('partner', $this->Partner->create());
        $this->render('form');
    }
    
    public function edit($id) {
        $this->set('partner', $this->Partner->findById($id));
        $this->render('form');
    }
    
    public function save() {
        $data = $this->data;
        if (! empty($data)) {
            
            $this->Partner->create($data);
            if (! $this->Partner->validates()) {
                $this->setFlash('Uneti podaci nisu validni');
                $this->set('partner', $data);
                return $this->render('form');
            }
            
            if (! preg_match('/^[fh]t+ps?:\/+/', $data['Partner']['url'])) {
                $data['Partner']['url'] = 'http://' . $data['Partner']['url'];
            }
            
            $toupload = false; $todelete = false;
            if (! $data['Partner']['image_new']['error']) {
                $toupload = $data['Partner']['image_new'];
                $todelete = $data['Partner']['image'];
                $data['Partner']['image'] = $data['Partner']['image_new']['name'];
            }
            
            if (! $this->Partner->save($data)) {
                $this->setFlash('Greška pri čuvanju podataka.');
                $this->set('partner', $data);
                return $this->render('form');
            }
            
            if ($toupload) {
                $dest = 'img' . DS . 'partners' . DS;
                $dest_file = $dest . $toupload['name'];
                if (upload($toupload['tmp_name'], $dest_file)) {
                    if ($todelete) {
                        @unlink($dest . $todelete);
                        @unlink($dest . hoverImage($todelete));
                    }
                    
                    copy($dest_file, hoverImage($dest_file));
                    grayscale($dest_file);
                }
            }
            
            $this->setFlash('Proizvođač je uspešno sačuvan.', 'ok');
            $this->redirect('edit/' . $this->Partner->id);
        }
        
        $this->redirect('index');
    }

    public function reorder() {
        $this->autoRender = false;
        $positions = $this->params->query['positions'];
        
        $data = array();
        foreach($positions as $pos) {
            $data[] = array(
                'Partner' => $pos
            );
        }
        
        if ($this->Partner->saveAll($data)) {
            echo 'Redosled je uspešno promenjen.';
        } else {
            echo 'Redosled nije promenjen.';
        }
    }
    
    public function delete($id) {
        if ($partner = $this->Partner->findById($id)) {
            if($this->Partner->delete($id)) {
                $filename = 'img' . DS . 'partners' . DS . $partner['Partner']['image'];
                @unlink($filename);
                @unlink(hoverImage($filename));
                
                $this->setFlash("Proizvođač je obrisan.", 'ok');
            } else {
                $this->setFlash("Greška pri brisanju proizvođača.");
            }
        }
        $this->redirect('index');
    }
}